<template>
  <div class="home">
    <!-- 登录、购物车 区域 -->
    <header>
      <div class="head">
        <ul>
          <li>
            <p v-show="!$store.state.user">
              <router-link to="" @click.native="loginDialog">登录</router-link>
              <span class="marg">|</span>
              <router-link to="" @click.native="dialogVisible">注册</router-link>
            </p>
            <!-- 退出登录 -->
            <p v-show="$store.state.user">
              欢迎
              <el-popover placement="top" width="160" v-model="visible">
                <p>确定退出登录吗</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="visible = false"
                    >取消</el-button
                  >
                  <el-button type="primary" size="mini" @click="outLogin">确定</el-button>
                </div>
                <span class="user" slot="reference">{{ $store.state.user }}</span>
              </el-popover>
            </p>
            <!-- 退出登录完 -->
          </li>
          <li>
            <router-link to="/order" @click.native="toPath">我的订单</router-link>
          </li>
          <li>
            <router-link to="/collect" @click.native="toPath">我的收藏</router-link>
          </li>
          <li class="cart">
            <router-link to="/shoppingCart" @click.native="toPath">
              <i class="el-icon-shopping-cart-full"></i>
              <span>购物车(0)</span>
            </router-link>
          </li>
        </ul>
      </div>
    </header>
    <!-- 内容区域 -->
    <div class="main_box">
      <div class="main">
        <!-- log -->
        <img src="../assets/imgs/logo.png" alt="" />
        <!-- 导航条 -->
        <el-menu :default-active="$route.path" mode="horizontal" router>
          <el-menu-item index="/homePage">首页</el-menu-item>
          <el-menu-item index="/allProducts">全部商品</el-menu-item>
          <el-menu-item index="/aboutUs">关于我们</el-menu-item>
        </el-menu>
        <!-- 搜索框 -->
        <el-input
          placeholder="请输入搜索内容"
          v-model="searchText"
          style="width: 300px; height: 35px"
        >
          <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
        </el-input>
      </div>
      <!-- 路由占位符 -->
      <div class="content">
        <keep-alive>
          <router-view></router-view>
        </keep-alive>
      </div>
    </div>
    <!-- 脚部 -->
    <footer>
      <div class="txt">
        <p><span class="icon"></span><span>7天无理由退换货</span></p>
        <p><span class="icon"></span><span>满99全场免邮</span></p>
        <p><span class="icon"></span><span>100%品质保证</span></p>
      </div>
      <div class="log">
        <img src="../assets/imgs/github.png" alt="" />
      </div>
      <div class="mod_help">
        <p>
          <router-link :to="'/homePage'">首页</router-link>
          <span>|</span>
          <router-link :to="'/allProducts'">全部商品</router-link>
          <span>|</span>
          <router-link :to="'/aboutUs'">关于我们</router-link>
        </p>
        <p>商城版权所有 © 2012-2021</p>
      </div>
    </footer>
    <!-- 注册组件 -->
    <register ref="dialog"></register>
    <!-- 登录组件 -->
    <login-name ref="login"></login-name>
  </div>
</template>

<script>
export default {
  name: "Home",

  data() {
    return {
      activeIndex: "/homePage", // 高亮
      searchText: "", // 搜索
      dialogFormVisible: false, // 对话框开关
      visible: false, //退出登录开关
    };
  },
  methods: {
    // 搜索
    search() {
      this.$router.push({ path: "/allProducts", query: { search: this.searchText } });
      this.searchText = "";
    },
    // 注册
    dialogVisible() {
      this.$refs.dialog.showDialog();
    },
    // 登录
    loginDialog() {
      this.$refs.login.showDialog();
    },
    // 退出登录
    outLogin() {
      this.$store.commit("outUser");
      this.$notify({
        title: "成功",
        message: "成功退出登录",
        type: "success",
      });
      this.visible = false;
    },
    // 登录鉴权
    toPath() {
      let user = this.$store.state.user;
      if (user === "") return this.$refs.login.showDialog();
    },
  },

  //   beforeRouteUpdate(to, from, next) {
  //     console.log(to)
  //     console.log(from)
  //     this.activeIndex = to.path
  //     next()
  //     // 在当前路由改变，但是该组件被复用时调用
  //     // 举例来说，对于一个带有动态参数的路径 /foo/:id，在 /foo/1 和 /foo/2 之间跳转的时候，
  //     // 由于会渲染同样的 Foo 组件，因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
  //     // 可以访问组件实例 `this`
  // }
};
</script>
<style lang="scss" scoped>
.home {
  height: 100%;
}

.container {
  width: 100%;
  height: 100%;
}

header {
  width: 100%;
  height: 40px;
  background-color: #3d3d3d;
  margin-bottom: 20px;
  .head {
    width: 85%;
    height: 100%;
    line-height: 40px;
    margin: 0 auto;
  }
  ul {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    li {
      height: 100%;
      color: #b0b0b0;
      font-size: 14px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 20px;
      .marg {
        margin: 0 5px;
      }
      a {
        color: #b0b0b0;
      }
      a:hover {
        color: #fff;
      }
    }
    .cart {
      width: 120px;
      background: #424242;
      i {
        margin-right: 5px;
      }
    }
    .cart:hover {
      background: #fff;
      color: #ff6700;
      a {
        width: 100%;
        height: 100%;
        text-align: center;
      }
      a:hover {
        color: #ff6700;
      }
    }
  }
}

.main_box {
  background-color: #fff;
  color: #333;
  .main {
    width: 85%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eee;
    background-color: #fff;
    img {
      height: 60px;
      width: 189px;
      margin-right: 100px;
    }
    .el-menu {
      flex: 1;
    }

    .el-input {
      margin-top: 10px;
    }
  }
}

footer {
  width: 100%;
  text-align: center;
  background: #2f2f2f;
  padding-bottom: 20px;
  color: #888;
  .txt {
    width: 100%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-content: center;
    border-bottom: 1px solid #3d3d3d;
    line-height: 145px;
    p {
      font-size: 20px;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
      .icon {
        width: 40px;
        height: 40px;
        background: url("../assets/imgs/us-icon.png") no-repeat;
      }
    }
  }
  .log {
    height: 50px;
    line-height: 50px;
    margin-top: 20px;
  }
  .mod_help {
    p {
      margin: 20px 0 16px 0;
      a {
        color: #888;
      }
      span {
        margin: 0 20px;
      }
    }
  }
}
.el-menu.el-menu--horizontal {
  border-bottom: 0;
}
.user {
  margin-left: 10px;
}
.user:hover {
  color: #fff;
}
</style>
